<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Thread
 *
 * @author Khoi
 */
class Thread {

    //put your code here
    public $id;
    public $replies;

    public static function getReplies($threadId, $streamId, $dateFrom, $dateTo, $index = 0, $length = 5) {
        $stream = Stream::withId($streamId);
        $dateFrom = str_replace('/', '-', $dateFrom);
        $dateTo = str_replace('/', '-', $dateTo);
        $reader = new ZMessageReader($dateFrom, $dateTo, 7, $stream->query, $stream->language);
        $reader->filterByThread($threadId);
        global $items, $itemTotal;
        $items = array();
        try {
            $reader->read(function($item, $total) use ($streamId) {
                global $items, $itemTotal;
                $reply = array();
                $reply['text'] = Signal::removeLink($item->getText());
                $reply['author'] = trim($item->getAuthor());
                $reply['likes'] = $item->getNumberOfLikes();
                $reply['id'] = $item->getId();
                $reply['threadId'] = $item->getThreadId();
                $reply['time'] = $item->getTime();
                $reply['sentiment'] = Sentiment::saveSentiment($item->getText(), $streamId, $item->getId(), $item->getThreadId(), $item->getTime(), 0);
                $reply['message'] = $item;
                $items[] = $reply;
                if ($total)
                    $itemTotal = $total;
            }, $index, $length);
        } catch (Exception $e) {
            return $e->getMessage();
        }
        $result = array();
        $result['items'] = $items;
        $result['total'] = $itemTotal;
        return $result;
    }

}
